/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package system;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import sql.SQLHandler;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Micke
 */
public class MovieHandler {

    SQLHandler sql;
    ArrayList<Movie> movies = new ArrayList<Movie>();
    Movie selectedMovie = null;
    UserHandler uH;


    public MovieHandler(UserHandler uH,SQLHandler sql) {
        this.sql=sql;
        this.uH = uH;
        if(!sql.isConnected()){
            sql.Connect();
        }
        getMovies();
        
        
    }

    public Movie getSelectedMovie() {
        return selectedMovie;
    }

    public String getMovieInfo(int movieID) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < movies.size(); i++) {
            if (movies.get(i).getMovieID() == movieID) {
                sb.append("Year of release: " + movies.get(i).getYearOfRelease());
                sb.append("\n");
                sb.append("Genre: " + movies.get(i).getGenre());
                sb.append("\n");
                sb.append("Runtime: " + movies.get(i).getRuntime()+" min.");
                sb.append("\n");
                sb.append("Budget: $" + movies.get(i).getBudget());
                sb.append("\n");
                sb.append("Language: " + movies.get(i).getLanguage());
                sb.append("\n");
                sb.append("Producer: " + movies.get(i).getProducer());
                sb.append("\n");
                sb.append("Director: " + movies.get(i).getDirector());
                sb.append("\n");
                sb.append("Cast: " + movies.get(i).getCast());
                sb.append("\n");
                sb.append("Grade: " + sql.getAverageGrade(movieID));
            }
        }

        return sb.toString();
    }

    public String getMoviePlot(int movieID) {
        for (int i = 0; i < movies.size(); i++) {
            if (movies.get(i).getMovieID() == movieID) {
                return movies.get(i).getPlot();
            }
        }
        return null;
    }

    public void setSelectedMovie(Movie selectedMovie) {
        this.selectedMovie = selectedMovie;
    }



    private void getMovies() {
        movies = sql.getMovies();
    }

    public String[] getTitleList() {
        String[] titles = new String[movies.size()];
        for (int i = 0; i < titles.length; i++) {
            titles[i] = movies.get(i).getTitle();

        }
        return titles;
    }

    public ArrayList<Movie> getMoviesArray() {
        return movies;
    }

    public String[] getTitleListFavourites(int userID) {
       
        return sql.getUserFavouriteTitles(userID);
    }



    

    public String getMovieTitleByMovieID(int movieID){
        for (int i = 0; i < movies.size(); i++) {
            if(movies.get(i).getMovieID()==movieID){
                return movies.get(i).getTitle();
            }
        }
        return null;
    }

    public Movie getMovieByTitle(String title) {
        for (int i = 0; i < movies.size(); i++) {
            if (movies.get(i).getTitle().equalsIgnoreCase(title)) {
                return movies.get(i);
            }
        }
        return null;
    }

    public int getCurrentUserID() {
        return uH.currentUser.getUserID();
    }

    public String geUserNameForUserID(int userID) {
        
        String ret = sql.getUserNameForUserID(userID);
        
        return ret;
    }
    public String[] getTop10List(){
        return sql.getTop10List();
    }
    public void addComment(String comment) {
        
        sql.addComment(getSelectedMovie().getMovieID(), getCurrentUserID(), comment);
        
    }

    public Comment[] getCommentsForSelectedMovieID() {
        
        int selMovieID;
        if (getSelectedMovie() != null){
            selMovieID = getSelectedMovie().getMovieID();
        } else {
            selMovieID = 0;
        }
        int lengtOfArray = sql.getLengthWhere("comments", "movieID = " + selMovieID);
        int overhead = 10;
        Comment[] comments = null;
        if (lengtOfArray > 0) {
            comments = new Comment[lengtOfArray + overhead];
            ResultSet rscom = sql.commentsForMovieID(selMovieID);
            int i = 0;
            try {
                while (rscom.next() && i < lengtOfArray + overhead) {
                    String dt = rscom.getString("datetime");
                    dt = dt.substring(0, dt.length()-5);
                    comments[i] = new Comment(rscom.getInt("movieID"), rscom.getInt("userID"), dt, rscom.getString("comment"));
                    i++;
                }
            } catch (SQLException e) {
                comments = new Comment[1];
                comments[0] = new Comment(-1, -1, sql.getDateTime(), "No comments available for this title.");
                JOptionPane.showMessageDialog(null, e);
            }
        } else {
            comments = new Comment[1];
            comments[0] = new Comment(-1, -1, sql.getDateTime(), "No comments available for this title.");
        }
        
        return comments;
    }
}
